<?php
declare (strict_types=1);

namespace app;

use app\console\model\WebSection;
use app\console\model\Banner;
use app\console\model\Member;
use think\facade\Request;
use app\console\model\WebCategory;
use app\console\model\WebArticle;
use think\facade\Session;
use think\facade\View;
use think\facade\Cache;
use think\facade\Db;
class WebController extends BaseController
{
    public function initialize()
    {
        $param = Request::param();
        $id = Request::param('id',1);
        $title = Request::param('title');//文章标题 搜搜使用

        $article_id = Request::param('article_id');
        if(Request::isMobile()){
            $perfix = 'wap';
        }else{
            $perfix = 'pc';
        }

        $member = Member::where([['id','=',Session::get('member_id')]])->find();

        //
//        if(in_array($id,[26,27,28,29])){
//            $w = (string)url('index',['id'=>8]);
//
//            redirect($w)->send();die;
//        }
       // $perfix = 'pc';
        //var_dump(Session::get('member_id'));
        /**
         * 全部分类树
         * 多级导航全部数据
         * 同级别导航数据
         */
        $WebCategory_all = WebCategory::where([['status','=','1']])->order(['sort' => 'asc', 'id' => 'asc'])->select()->toArray();
        $nav_all_data = getMenuList($WebCategory_all, 0);
        View::assign('nav_all',$nav_all_data);

        $parent_id  = WebCategory::where([['id','=',$id]])->value('parent_id');//58
        View::assign('parent_id',$parent_id);
        $nav_together = WebCategory::where([['parent_id','=',$parent_id]])->order(['sort' => 'asc', 'id' => 'asc'])->select();
        View::assign('nav_together',$nav_together);

        $parent_id_id = WebCategory::where([['id','=',$parent_id]])->value('parent_id');

        $parent_nav_together = WebCategory::where([['parent_id','=',$parent_id_id]])->order(['sort' => 'asc', 'id' => 'asc'])->select();

        View::assign('parent_nav_together',$parent_nav_together);

        $title  = WebCategory::where([['id','=',$id]])->value('title');
        View::assign('title',$title);
        $en_title  = WebCategory::where([['id','=',$id]])->value('en_title');
        View::assign('en_title',$en_title);


        $section = WebArticle::where('cat_id',$id)->order(['sort'=>'asc','id'=>'desc'])->group('section')->column('section');
        View::assign('section',$section);

        /**
         * 获取当前的实际分类
         */
        if($article_id){
            $cat_id = WebArticle::where([['id','=',$article_id]])->value('cat_id'); //只要文档ID存在就有优先覆盖 用一个find无的时候会报错啊
        }else{
            $cat_id = $id;
        }

        /**
         * 分类类型
         */

       Session::set('template',$perfix.'_index');

        //"../app/pc/view/{$template}"
        //绝对路径必须带.html 不是的需要去掉.html
        if($id && empty($article_id)){

            //Session::delete('template');直接覆盖就行 不需要删除啊
            $son_first_id = WebCategory::where([['parent_id','=',$id]])->order(['sort'=>'asc','id'=>'asc'])->value('id');

            $WebCategory_find = WebCategory::where([['id','=',$id]])->find();

            if($WebCategory_find['type_id'] == 3){ //无内容

                $url = url('index',['id'=>$son_first_id]);

            redirect((string) url('index',['id' => $son_first_id]))->send();die;

            }elseif ($WebCategory_find['type_id'] == 1){ //单页
                 View::assign('WebCategory_data',$WebCategory_find);

                 Session::set('template',$perfix.'_'.$WebCategory_find['template_single']);
            }elseif($WebCategory_find['type_id'] == 2){ //列表

               // Session::set('template',$WebCategory_find[$perfix.'_template_list']);
                Session::set('template',$perfix.'_'.$WebCategory_find['template_list']);
                //搜索数据

                $article_paginate = WebArticle::where([['cat_id','=',$id],['status','=',1]])->where([['title','like','%'.$title.'%']])->order(['sort' => 'asc','id' => 'desc'])->paginate(['query' => $param,'list_rows' => 10]);

                View::assign('article_data',$article_paginate);
            }

        }else{
            //文章不为空前提 栏目存在和栏目不存在
            $article_find = WebArticle::where([['id','=',$article_id]])->find();
            $WebCategory_find = WebCategory::where([['id','=',$article_find['cat_id']]])->find();

            Session::set('template',$perfix.'_'.$WebCategory_find['template_view']);
            View::assign('id',$WebCategory_find['id']);
            View::assign('article_title',$WebCategory_find['title']);
        }










        /**
         * 子集分类树
         * 当前分类下的子集分类
         */
        $nav_son_data = getMenuList($WebCategory_all,$id);
        View::assign('nav_son',$nav_son_data);


        /**
         * 通用面包屑导航  1文章 2分类
         * 文章模板一级分页数据
         */

        if($article_id){
            //文档ID不存在用分类ID 反之使用文档的

            $parent_path = parentPath($cat_id,'WebCategory',true);//文档面包屑导航

            View::assign('parent_path',$parent_path);
            $info = WebArticle::where([['id','=',$article_id]])->find();

//            if($member_level > $info['level_id']){
//                $info = WebArticle::where([['id','=',$article_id]])->find();
//            }else{
//                $info = [];
//            }

            $info->inc('number')->update();
            View::assign('info',$info);

            $WebCategory_find = WebCategory::where([['id','=',$info['cat_id']]])->find();

           // Session::set('template',$WebCategory_find['template_view']);

            $all = WebArticle::where('cat_id', $info['cat_id'])->order(['sort' => 'asc', 'id' => 'desc'])->select();

            $artcile_k = 0;
            $start = 0;
            $end = $all->count()-1;

            foreach ($all as $k => $v){
                if(input('article_id') == $v['id']){
                    $artcile_k = $k;
                }
            }
            if($artcile_k-1 >= 0){ //上一条
                $prev = $all[$artcile_k-1];
                $prev['a'] = "<a class=\"prev\" href='" . url(Request::action(), ['article_id' => $prev['id'],'id' => input('id'),'t_id' => input('t_id'),'th_id' => input('th_id')]) . "'>" . '上一篇:' . $prev['title'] . "</a>";
            }else{
                $prev['a'] = '<a class="prev" href="javascript:voild(0)">没有了</a>';
            }
            // $prev['a'] = $prev ? "<a class=\"xqy0_con_btomnra\" href='" . url('details', ['article_id' => $prev['id']]) . "'>" . '上一篇:' . $prev['title'] . "</a>" : '<a class="xqy0_con_btomnra" href="javascript:voild(0)">没有了</a>';
            View::assign('prev', $prev);

            //$next = (new Article())->where('type_id', $info['type_id'])->where('sort', '>=', $sort)->order(['sort' => 'asc', 'id' => 'asc'])->limit(1)->find();

            if($artcile_k+1 <= $end){ //0-7

                $next = $all[$artcile_k+1];
                $next['a'] = "<a class=\"next\" href='" . url(Request::action(), ['article_id' => $next['id'],'id' => input('id'),'t_id' => input('t_id'),'th_id' => input('th_id')]) . "'>" . '下一篇:' . $next['title'] . "</a>";
            }else{
                $next['a'] = '<a class="next" href="javascript:voild(0)">没有了</a>';
            }
            // $next['a'] = $next ? "<a class=\"xqy0_con_btomnrb\" href='" . url('details', ['article_id' => $next['id']]) . "'>" . '下一篇:' . $next['title'] . "</a>" : '<a class="xqy0_con_btomnrb" href="javascript:voild(0)">没有了</a>';
            View::assign('next', $next);

        }else{
            $parent_path = parentPath($cat_id,'WebCategory',true);//分类面包屑导航
            View::assign('parent_path',$parent_path);
        }


        //$banner_data = Banner::where([['cat_id','=',input('id',1)]])->order(['cat_id' => 'asc','sort' => 'asc','id' => 'asc'])->select();

       // View::assign('banner_data',$banner_data);





    }


}